﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Function;

namespace UI.Permission
{
    public class ButtonHref
    {
        /// <summary>
        /// 编辑页面链接 保留当前页面查询条件
        /// </summary>
        /// <param name="entityid">实体id</param>
        /// <param name="Page">页面地址</param>
        /// <param name="Text">超链接文本</param>
        /// <param name="predict">筛选条件 如果不满足条件则返回空字符串</param>
        /// <returns></returns>
        public static string EditHref(string entityid, string Page = "Edit.aspx", string Text = "编辑", Func<Boolean> predict = null)
        {
            if (predict != null)
            {
                if (!predict())
                {
                    return "";
                }
            }
            if (!RightCheck.HavePermission(RightCheck.permissionType.Edit))
            {
                return "";
            }
            HttpRequest Request = HttpContext.Current.Request;
            string[] urlKeys = Request.QueryString.AllKeys;
            string newUrl = "";
            string tmpvalue = "";
            foreach (string key in urlKeys)
            {
                if (key != "entityid")
                {
                    tmpvalue = Request.QueryString[key];
                    tmpvalue = HttpContext.Current.Server.UrlEncode(tmpvalue);
                    newUrl += newUrl == "" ? ("?" + key + "=" + tmpvalue) : ("&" + key + "=" + tmpvalue);
                }
            }
            newUrl += newUrl == "" ? ("?entityid=") : ("&entityid=");
            return string.Format("<a href='{0}{1}{2}'>{3}</a>", Page, newUrl, entityid, Text);
        }

        /// <summary>
        /// 删除地址
        /// </summary>
        /// <param name="entityid">实体id</param>
        /// <param name="Page">跳转页面</param>
        /// <param name="Text">超链接文本</param>
        /// <param name="ConfirmInformation">确认信息</param>
        /// <param name="predict">筛选条件 如果不满足条件则返回空字符串</param>
        /// <returns></returns>
        public static string DelHref(string entityid, string Page = "Delete.aspx", string Text = "删除", string ConfirmInformation = "确定要删除吗?", Func<Boolean> predict = null)
        {
            if (predict != null)
            {
                if (!predict())
                {
                    return "";
                }
            }
            //检查权限
            if (!RightCheck.HavePermission(RightCheck.permissionType.Delete))
            {
                return "";
            }
            HttpRequest Request = HttpContext.Current.Request;
            string[] urlKeys = Request.QueryString.AllKeys;
            string newUrl = "";
            string tmpvalue = "";
            foreach (string key in urlKeys)
            {
                if (key != "entityid")
                {
                    tmpvalue = Request.QueryString[key];
                    tmpvalue = HttpContext.Current.Server.UrlEncode(tmpvalue);
                    newUrl += newUrl == "" ? ("?" + key + "=" + tmpvalue) : ("&" + key + "=" + tmpvalue);
                }
            }
            newUrl += newUrl == "" ? ("?entityid=") : ("&entityid=");
            return string.Format("<a onclick=\"return confirm('{0}?')\" href='{1}{2}{3}'>{4}</a>", ConfirmInformation, Page, newUrl, entityid, Text);
        }

        /// <summary>
        /// 详细链接
        /// </summary>
        /// <param name="entityid">实体id</param>
        /// <param name="Page">跳转页面</param>
        /// <param name="Text">抄链接显示文本</param>
        /// <returns>超链接地址</returns>
        /// <param name="predict">筛选条件 如果不满足条件则返回空字符串</param>
        public static string DetailHref(string entityid, string Page = "Detail.aspx", string Text = "详细", Func<Boolean> predict = null)
        {
            if (predict != null)
            {
                if (!predict())
                {
                    return "";
                }
            }
            //检查权限
            if (!RightCheck.HavePermission(RightCheck.permissionType.View))
            {
                return "";
            }
            HttpRequest Request = HttpContext.Current.Request;
            string[] urlKeys = Request.QueryString.AllKeys;
            string newUrl = "";
            string tmpvalue = "";
            foreach (string key in urlKeys)
            {
                if (key != "entityid")
                {
                    tmpvalue = Request.QueryString[key];
                    tmpvalue = HttpContext.Current.Server.UrlEncode(tmpvalue);
                    newUrl += newUrl == "" ? ("?" + key + "=" + tmpvalue) : ("&" + key + "=" + tmpvalue);
                }
            }
            newUrl += newUrl == "" ? ("?entityid=") : ("&entityid=");
            return string.Format("<a href='{0}{1}{2}'>{3}</a>", Page, newUrl, entityid, Text);
        }

        /// <summary>
        /// 自定义链接
        /// </summary>
        /// <param name="entityid">实体id</param>
        /// <param name="Page">跳转页面</param>
        /// <param name="Text">链接显示文本</param>
        /// <param name="RightTag">自定义权限标识</param>
        /// <param name="Prediction">筛选器 不符合条件返回空值</param>
        /// <returns>跳转地址</returns>
        public static string CustomHref(string entityid, string Page, string Text, string RightTag, bool Prediction = true, string clickEvent = "", string entityIDText = "entityid")
        {
            if (!Prediction)
            {
                return "";
            }
            //检查权限
            if (RightTag.IsNotEmpty())
            {
                if (!RightCheck.HavePermission(RightTag))
                {
                    return "";
                }
            }
            HttpRequest Request = HttpContext.Current.Request;
            string[] urlKeys = Request.QueryString.AllKeys;
            string newUrl = "";
            string tmpvalue = "";
            foreach (string key in urlKeys)
            {
                if (key != entityIDText)
                {
                    tmpvalue = Request.QueryString[key];
                    tmpvalue = HttpContext.Current.Server.UrlEncode(tmpvalue);
                    newUrl += newUrl == "" ? ("?" + key + "=" + tmpvalue) : ("&" + key + "=" + tmpvalue);
                }
            }
            newUrl += newUrl == "" ? ("?" + entityIDText + "=") : ("&" + entityIDText + "=");
            if (clickEvent.IsNotEmpty())
            {
                return string.Format("<a onclick=\"{4}\" href='{0}{1}{2}'>{3}</a>", Page, newUrl, entityid, Text, clickEvent);
            }
            else
            {
                return string.Format("<a href='{0}{1}{2}'>{3}</a>", Page, newUrl, entityid, Text);
            }
        }
    }
}